package daoImpl;

import baseDao.BaseDao;
import dao.PostDao;
import entity.Post;
import javafx.geometry.Pos;

import java.sql.*;
import java.util.List;

/**
 * Created by miaoyu on 2017/7/12.
 */
public class PostDaoImpl extends BaseDao implements PostDao {
    // Date now = Calendar.getInstance().getTime();
    //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //    post.setTime(sdf.format(now));

    /**
     * 发布帖子
     * @param post Post对象
     * @return改变行数
     * 成功
     */
    public int addPost(Post post) {
        Object objs[] = {post.getTopicID(),post.getTitle(),post.getUserID(),post.getTime(),post.getContent()};
        return executeUpdate("insert into post(topicID,title,userID,time,content) values(?,?,?,?,?)",objs);
    }

    /**
     * 删除帖子
     * @param postID 帖子ID
     * @return 改变行数
     * 成功
     */
    public int deletePost(int postID) {
        return executeUpdate("delete from post where postID ="+postID,null);
    }



    /**
     * 获取一个用户发的所有帖子
     * @param username
     * @return List<Post>
     */
    public List<Post> queryAllPostsByUsername(String username){

        List<Post> list = executeQuery("SELECT * FROM post WHERE userID = (SELECT userID FROM user WHERE username = '"+username+"')",null,Post.class);

        return list;
    }

    public List<Post> queryAllCollectionsByUsername(String username){
        List<Post> list = executeQuery("SELECT * FROM post WHERE postID IN (SELECT postID FROM collection WHERE username ='"+username+"')",null,Post.class);
        return list;
    }

    public List<Post> queryAllPostByCategory(String category){
        List<Post> list = executeQuery("SELECT * FROM post WHERE topicID = (SELECT topicID FROM topic WHERE topicName = '"+category+"')",null,Post.class);
        return list;
    }




















    public int getPostID(int userID,String time) {
        int id = 0;










//        Connection conn = null;
//        ResultSet rs = null;
//        Statement stmt = null;
//        try {
//            //第一步:注册驱动
//            Class.forName("com.mysql.jdbc.Driver");
//            conn = DriverManager.getConnection("jdbc:mysql://192.168.153.17:3306/giggle", "admin", "admin");
//            stmt = conn.createStatement();
//            rs = stmt.executeQuery("SELECT floorID FROM floor where userID ="+userID+" AND time='"+time+"'");
//            while (rs.next()){
//                id = rs.getInt("floorID");
//            }
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }catch (SQLException e) {
//            e.printStackTrace();
//        }finally {
//            try {
//                if (rs != null)
//                    rs.close();
//                if (stmt != null)
//                    stmt.close();
//                if (conn != null)
//                    conn.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//                e.printStackTrace();
//            }
//        }
        return id;
    }
}